package com.winchain.chainbill.bill.biz.receive.cmd;

import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.bee.base.utils.DateUtil;
import com.shell.bee.base.utils.MoneyUtil;
import com.shell.bee.base.utils.SpringContextUtil;
import com.shell.feign.PubUserFeign;
import com.shell.framework.command.CallbackContext;
import com.shell.framework.command.CommandContext;
import com.shell.framework.command.ICallback;
import com.shell.framework.command.ICommand;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.MessageSender;
import com.shell.governor.modules.message.keyword.SmsRcvAprvPassSellerKeyword;
import com.shell.governor.modules.message.keyword.SysRcvAprvPassBillKeyword;
import com.shell.governor.modules.message.keyword.SysRcvAprvPassBuyerKeyword;
import com.shell.governor.modules.message.keyword.SysRcvAprvPassSellerKeyword;
import com.shell.governor.modules.message.keyword.SysRcvAprvRejectKeyword;
import com.shell.governor.modules.message.keyword.SysRcvAprvRjctKeyword;
import com.shell.governor.user.po.PubUser;
import com.winchain.chainbill.bill.base.po.CbOpt;
import com.winchain.chainbill.bill.base.po.CbRcv;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.base.po.CbSnd;
import com.winchain.chainbill.bill.base.po.CbXfe;
import com.winchain.chainbill.bill.base.service.CbSndService;
import com.winchain.chainbill.bill.base.service.CbXfeService;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.send.service.BillMsgService;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillRcvMsgCmd implements ICommand<Bill<CbRcv>>, ICallback<Bill<CbRcv>, AcctTxRec> {
	@Autowired
	private BillMsgService billMsgService;
	@Autowired
	private PubUserFeign userFeign;
	@Autowired
	private CbXfeService cbXfeService;
	@Autowired
	private CbSndService cbSndService;

	public boolean execute(CommandContext<Bill<CbRcv>> context) throws Exception {
		CbRec cbRec = ((Bill) context.getSource()).getRec();
		CbRcv cbRcv = (CbRcv) ((Bill) context.getSource()).getSource();

		String bizType = (String) context.getValue("bizType");
		if ("cb_rcv".equals(bizType)) {
			String template = null;
			SysRcvAprvRejectKeyword sysRcvAprvRejectKeyword = new SysRcvAprvRejectKeyword();
			sysRcvAprvRejectKeyword.setBillAmt(String.valueOf(cbRec.getBillAmt()));
			sysRcvAprvRejectKeyword.setRcvDate(DateUtil.parseDate(cbRcv.getRcvDate(), "yyyyMMdd", "yyyy-MM-dd"));

			CbOpt cbOpt = (CbOpt) context.getValue("Operation");
			sysRcvAprvRejectKeyword.setRemark(cbOpt.getRemark());

			List<String> userIds = new ArrayList();
			if ("0".equals(cbRcv.getTxType())) {
				String userIdOpt = this.billMsgService.getOprId(cbRec.getBillId(), "S000");
				String userIdRev = this.billMsgService.getOprId(cbRec.getBillId(), "S001");

				template = "MT02207";

				userIds.add(userIdOpt);
				userIds.add(userIdRev);
			} else if ("1".equals(cbRcv.getTxType())) {
				CbXfe cbXfe = (CbXfe) this.cbXfeService.selectByKey(cbRcv.getTxId());

				String userIdOpt = "";
				template = "MT02208";
				if (cbXfe.getBillId().equals(cbXfe.getPrntId())) {
					userIdOpt = this.billMsgService.getOprId(cbRec.getBillId(), "S005");
				} else {
					userIdOpt = this.billMsgService.getOprId(cbRec.getBillId(), "S000");
				}
				String userIdRev = this.billMsgService.getOprId(cbRec.getBillId(), "S006");
				userIds.add(userIdOpt);
				userIds.add(userIdRev);
			}
			MessageSender.sendMessage(String.join(",", userIds), CommonDataUtil.getUserId(), template,
					sysRcvAprvRejectKeyword);
		} else if ("cb_rc_aprv".equals(bizType)) {
			String senderId = this.billMsgService.getOprId(cbRec.getBillId(), "S025");

			sendSeller(cbRec, cbRcv, senderId);

			sendBuyer(cbRec, cbRcv, senderId);
			if ("1".equals(cbRcv.getTxType())) {
				sendSnd(cbRec, cbRcv, senderId);
			}
		} else if ("rc_aprv_rj".equals(bizType)) {
			String template = "MT01306";
			SysRcvAprvRjctKeyword keyWord = new SysRcvAprvRjctKeyword();
			keyWord.setBillAmt(String.valueOf(cbRec.getBillAmt()));
			keyWord.setSndDate(DateUtil.parseDate(cbRec.getSndDate(), "yyyyMMdd", "yyyy-MM-dd"));

			CbOpt cbOpt = (CbOpt) context.getValue("Operation");
			keyWord.setRemark(cbOpt.getRemark());
			keyWord.setSellerNm(cbRcv.getSellerNm());

			String senderId = CommonDataUtil.getUserId();
			String rcvId = cbRcv.getBuyerId();
			MessageSender.sendMessageByTmpl(senderId, template, keyWord, rcvId);
		}
		return false;
	}

	public boolean callback(CallbackContext<Bill<CbRcv>, AcctTxRec> context) throws Exception {
		execute(context);
		return false;
	}

	private void sendSeller(CbRec cbRec, CbRcv cbRcv, String oprId) throws Exception {
		String template = "MT01303";
		SysRcvAprvPassSellerKeyword keyWord = new SysRcvAprvPassSellerKeyword();
		keyWord.setBillAmt(String.valueOf(cbRec.getBillAmt()));
		keyWord.setBuyerNm(cbRcv.getBuyerNm());
		keyWord.setSndDate(DateUtil.parseDate(cbRcv.getCreateTime(), "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd"));

		String senderId = CommonDataUtil.getUserId();
		String rcvId = cbRcv.getSellerId();
		MessageSender.sendMessageByTmpl(senderId, template, keyWord, rcvId);

		template = "MT02203";
		SmsRcvAprvPassSellerKeyword smsRcvAprvPassSellerKeyword = new SmsRcvAprvPassSellerKeyword();
		smsRcvAprvPassSellerKeyword.setBillAmt(String.valueOf(cbRec.getBillAmt()));
		smsRcvAprvPassSellerKeyword.setBuyerNm(cbRcv.getBuyerNm());
		smsRcvAprvPassSellerKeyword
				.setSndDate(DateUtil.parseDate(cbRcv.getCreateTime(), "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd"));
		smsRcvAprvPassSellerKeyword.setExpDate(DateUtil.parseDate(cbRec.getExpDate(), "yyyyMMdd", "yyyy-MM-dd"));
		smsRcvAprvPassSellerKeyword.setUrl(SpringContextUtil.getProperty("chainbill.website"));

		MessageSender.sendMessageByTmpl(senderId, template, smsRcvAprvPassSellerKeyword, rcvId);
	}

	private void sendBuyer(CbRec cbRec, CbRcv cbRcv, String oprId) throws Exception {
		String template = "MT01304";
		SysRcvAprvPassBuyerKeyword keyWord = new SysRcvAprvPassBuyerKeyword();
		keyWord.setSellerNm(cbRcv.getSellerNm());
		keyWord.setBillAmt(String.valueOf(cbRec.getBillAmt()));
		keyWord.setSndDate(cbRcv.getCreateTime());

		PubUser pubUser = this.userFeign.selectByKey(oprId);
		keyWord.setUserName(pubUser.getUserName());

		String senderId = CommonDataUtil.getUserId();
		if ("0".equals(cbRcv.getTxType())) {
			oprId = this.billMsgService.getOprIdByBillIdAndState(cbRec.getBillId(), "S003");
			pubUser = this.userFeign.selectByKey(oprId);
			keyWord.setUserName(pubUser.getUserName());

			String rcvId = cbRcv.getBuyerId();
			MessageSender.sendMessageByTmpl(senderId, template, keyWord, rcvId);
		} else if ("1".equals(cbRcv.getTxType())) {
			String transferId = cbRcv.getBuyerId();
			MessageSender.sendMessageByTmpl(senderId, template, keyWord, transferId);
		}
	}

	private void sendSnd(CbRec cbRec, CbRcv cbRcv, String oprId) throws Exception {
		CbSnd cbSend = this.cbSndService.getBillSndById(cbRec.getRootId());

		String template = "MT01305";
		SysRcvAprvPassBillKeyword keyWord = new SysRcvAprvPassBillKeyword();
		keyWord.setBillAmt(String.valueOf(cbRec.getBillAmt()));
		keyWord.setBillAmtA(MoneyUtil.number2CNMoney(cbRec.getBillAmt()));
		keyWord.setBillCode(cbRec.getBillCode());
		keyWord.setBuyerNm(cbSend.getBuyerNm());
		keyWord.setSellerNm(cbRcv.getSellerNm());
		Calendar c = Calendar.getInstance();
		keyWord.setXfeNm(cbRcv.getBuyerNm());
		keyWord.setYear(String.valueOf(c.get(1)));
		keyWord.setMonth(String.valueOf(c.get(2) + 1));
		keyWord.setDay(String.valueOf(c.get(5)));

		String senderId = CommonDataUtil.getUserId();
		String sendId = cbSend.getBuyerId();
		MessageSender.sendMessageByTmpl(senderId, template, keyWord, sendId);
	}
}
